package com.lindenvalley.de.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lindenvalley.de.LearnActivity;
import com.lindenvalley.de.R;
import com.lindenvalley.de.content.Answer;
import com.lindenvalley.de.content.AnswerResult;
import com.lindenvalley.de.content.Question;
import com.lindenvalley.de.content.Section;
import com.lindenvalley.de.database.DbSectionsHelper;



public class ReportArrayAdapter extends ArrayAdapter<Question>{
	
	private Context mContext;
	private LayoutInflater inflater;
	private final String TAG = this.getClass().getSimpleName();
	private Cursor mCursor;
	private int resource;
	private List<Question> mQuestions;
	private int themeId, roundNumber;
	private String[] columns = new String[]{DbSectionsHelper.QUESTION, DbSectionsHelper.ANSWER_TYPE, DbSectionsHelper.ANSWERS};
	private Map<Integer, AnswerResult> results = new HashMap<Integer, AnswerResult>();
	
	public ReportArrayAdapter(Context context, int resource, List<Question> questions, int themeId, int roundNumber){
		super(context, resource, questions);
		this.mContext = context;	
		this.resource = resource;
		this.mQuestions = questions;
		this.themeId = themeId;
		this.roundNumber = roundNumber;
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		DbSectionsHelper sectionsHelper = new DbSectionsHelper(mContext);
		SQLiteDatabase database = sectionsHelper.getReadableDatabase();
		mCursor = database.query(
					DbSectionsHelper.TABLE_NAME, 
					columns, DbSectionsHelper.ROUND + "=? and " + DbSectionsHelper.THEME_ID + "=? and " + DbSectionsHelper.IS_EXAM +"=1", 
					new String[]{String.valueOf(roundNumber), String.valueOf(themeId)}, null, null, null);
		mCursor.moveToFirst();
		do{
			AnswerResult result = new AnswerResult();
			result.setAnswerType(mCursor.getInt(1));
			result.setAnswer(mCursor.getString(2));
			results.put(mCursor.getInt(0), result);
		}while(mCursor.moveToNext());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (convertView == null){
			convertView = inflater.inflate(resource, parent, false);
		}
				
		Question question = mQuestions.get(position);
		TextView tvName = (TextView) convertView.findViewById(R.id.reportThemeText);		
		TextView tvNumber = (TextView) convertView.findViewById(R.id.reportTextNumber);
		AnswerResult result = results.get(position);
		if (result.getAnswerType() <= 0){
			tvName.setCompoundDrawablesWithIntrinsicBounds(null, null, mContext.getResources().getDrawable(R.drawable.report_incorrect_answer), null);
		} else {
			tvName.setCompoundDrawablesWithIntrinsicBounds(null, null, mContext.getResources().getDrawable(R.drawable.marker), null);
		}
		LinearLayout layoutCorrectAnswer = (LinearLayout) convertView.findViewById(R.id.reportLayoutCorrectAnswer);
		layoutCorrectAnswer.setVisibility(View.VISIBLE);
		String[] answers= null;
		try{
			answers = result.getAnswer().split(",");
			TextView yourAnswer = (TextView) convertView.findViewById(R.id.reportYourAnswer);
			for (int i = 0; i < answers.length; i++){
				try{
					int answerId = Integer.parseInt(answers[i]);
					if (answerId == -1){
						yourAnswer.setText(R.string.reportNotAnswer);
						yourAnswer.setTextColor(mContext.getResources().getColor(R.color.colorAnswerYellow));
						continue;
					}
					Answer answerById = question.getAnswerById(answerId);
					yourAnswer.setText(answerById.getDescription());
					switch (result.getAnswerType()) {					
						case LearnActivity.CORRENT_ANSWER:						
							yourAnswer.setTextColor(mContext.getResources().getColor(R.color.colorAnswerGreen));
							layoutCorrectAnswer.setVisibility(View.GONE);
							break;
						case LearnActivity.INCORRECT_ANSWER:
							yourAnswer.setTextColor(mContext.getResources().getColor(R.color.colorAnswerRed));
							break;
					
					}
				} catch (NumberFormatException ex){
					
				}
			}
		} catch (NullPointerException ex){
			
		}
		
		tvName.setText(Html.fromHtml(question.getDescription()));
		tvNumber.setText(position+1 + "");
		
		return convertView;
	}

}
